package com.atguigu.schedule.exception;

import com.atguigu.schedule.common.Result;
import com.atguigu.schedule.common.ResultCodeEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 
 * @author atguigu
 * @since 2024
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理运行时异常
     * 
     * @param e 异常
     * @return 错误结果
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<String> handleRuntimeException(RuntimeException e) {
        return Result.build(null, ResultCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }

    /**
     * 处理所有异常
     * 
     * @param e 异常
     * @return 错误结果
     */
    @ExceptionHandler(Exception.class)
    public Result<String> handleException(Exception e) {
        e.printStackTrace(); // 打印异常堆栈
        return Result.build(null, ResultCodeEnum.SYSTEM_ERROR.getCode(), "系统异常: " + e.getMessage());
    }
}
